var CM = window.CM || {};

CM = {

    //sasazaixian 2017-03-20 增加文单服务器默认路径
    getDownLoadFileUrl: function () {
        return "/fileserver/common/";
    },
    /**
     * 获取地址栏的值
     *
     * @param name
     *            需要取值的名
     * @returns 返回取到的值
     */
    getUrlValue: function (name, url) {//修正了url参数如果是&pid=a&pidname=c这种参数获去的bug；sasazaixian 2017-03-20
        var num = url.indexOf("?")
        url = url.substr(num + 1); //取得所有参数   stringvar.substr(start [, length ]
        var arr = url.split("&"); //各个参数放到数组里
        for (var i = 0; i < arr.length; i++) {
            num = arr[i].indexOf("=");
            if (num > 0 && arr[i].substring(0, num) == name) {
                return value = arr[i].substr(num + 1);
            }
        }
        return null;
    },
    getSelectOption: function (selectId, url, copyId, value) {
        if (selectId == "" || selectId == undefined || url == "" || url == undefined) {
            mini.alert(selectId + "参数有误");
            return;
        }
        var str = "";
        var select = document.getElementById(selectId);
        CM.delAllOption(select);
        $.ajax({
            url: url,
            type: "post",
            data: str,
            async: false,
            dataType: 'json',
            timeout: 10000,
            error: function (data) {
                mini.alert(selectId + "获取信息失败");
            },
            success: function (data) {
                if (data.message == "操作失败") {
                    mini.alert(selectId + "获取下拉列表信息失败");
                }
                var obj = data.items;
                console.log(obj);
                if (obj) {
                    for (i = 0; i < obj.length; i++) {
                        var myOption = document.createElement("option");
                        myOption.value = obj[i][0];
                        myOption.text = obj[i][1];
                        select.add(myOption);
                    }
                }
                if (value) {
                    $("#" + selectId).val(value);
                }
            }
        })
    },
    /**
     * 清除选择框里面的值
     */
    delAllOption: function (obj) {
        if (!obj) {
            $(this).alertmsg("没有找到对应的下拉列表");
            return;
        }
        var j = obj.length;
        if (j == 0) {
            return;
        }
        if (j == 1 && obj.options[0].value == "") {
            return;
        }
        for (var i = j - 1; i > 0; i--) {
            obj.remove(i);
        }

    },
    getCheckBox: function (selectId, url, name, value, className) {
        if (selectId == "" || selectId == undefined || url == "" || url == undefined) {
            mini.alert(selectId + "参数有误");
            return;
        }
        var str = "";
        var select = document.getElementById(selectId);
        select.innerHTML = "";


        getServerInf(url, {}, function (data) {
            if (data.message == "操作失败") {
                mini.alert(selectId + "获取信息失败");
            } else {
                var obj = data.items;
                if (obj) {
                    for (i = 0; i < obj.length; i++) {
                        if (value && value.indexOf(obj[i][0]) > -1) {
                            str += "<input name='" + name + "' value='" + obj[i][0] + "' class='mini-CheckBox' checked=true>";
                        } else {
                            str += "<input name='" + name + "' value='" + obj[i][0] + "' class='mini-CheckBox'>";
                        }
                        str += "<span class='lbl " + name + obj[i][0] + "' > " + obj[i][1] + "</span>&emsp;";
                    }
                    select.innerHTML = str;
                }
            }
        }, null, null, true);
    },
    getRadio: function (selectId, url, name, value, className) {
        if (selectId == "" || selectId == undefined || url == "" || url == undefined) {
            mini.alert(selectId + "参数有误");
            return;
        }
        var str = "";
        var select = document.getElementById(selectId);
        select.innerHTML = "";
        getServerInf(url, {}, function (data) {
            if (data.message == "操作失败") {
                mini.alert(selectId + "获取信息失败");
            } else {
                var obj = data.items;
                if (obj) {
                    for (i = 0; i < obj.length; i++) {
                        if (value && value == obj[i][0]) {
                            str += "<input name='" + name + "' value='" + obj[i][0] + "' type='radio' checked=true>";
                        } else {
                            str += "<input name='" + name + "'  value='" + obj[i][0] + "' type='radio'>";
                        }
                        str += "<span class='lbl " + name + obj[i][0] + "'> " + obj[i][1] + "</span>&emsp;";
                    }
                    select.innerHTML = str
                }
            }
        }, null, null, true);
    },
    getCombobox: function (ComboboxId, url) {
        var Combobox = mini.get(ComboboxId);
        getServerInf(url, {}, function (data) {
            if (data.statusCode && data.statusCode != '200') {
                alert(data.message);
                return;
            }
            Combobox.setData(data.list);
        }, null, null, true);
    },
    getTree: function (treeId, url, id, pid, callBack) {
        var tree = mini.get(treeId)
        getServerInf(url, {}, function (data) {
            if (data.statusCode && data.statusCode != '200') {
                mini.alert(data.message);
                return;
            }
            //tree = $.fn.zTree.init(tree, setting, data.treeList);
            tree.loadList(data.treeList, id, pid);
            if (callBack) {
                eval(callBack + "()");
            }
        }, null, null, true);

    },
    addTreeRoot: function (url, data, treeObj) {
        getServerInf("sysFunction!add.do", data, function (data) {
            if (data.OK == "OK") {
                refershTree();
            } else {
                mini.alert(data.message);
            }
        }, null, '新增中。。。', true);
    },
    addTreeCurrent: function (url, data, treeObj) {
        getServerInf(url, data, function (data) {
            if (data.OK == "OK") {
                refershTree();
                treeObj.selectNode(currentTreeNode);
                currentTreeNode = treeObj.getSelectedNode();
                var pnode = treeObj.getParentNode(currentTreeNode);
                while (pnode[treeObj.idField]) {
                    treeObj.expandNode(pnode);
                    pnode = treeObj.getParentNode(pnode);
                }
            } else {
                mini.alert(data.message);
            }
        }, null, '新增中。。。', true);
    },
    addTreesub: function (url, data, treeObj) {
        getServerInf(url, data, function (data) {
            if (data.OK == "OK") {
                refershTree();
                treeObj.selectNode(currentTreeNode);
                currentTreeNode = treeObj.getSelectedNode();
                treeObj.expandNode(currentTreeNode);
                var pnode = treeObj.getParentNode(currentTreeNode);
                while (pnode[treeObj.idField]) {
                    treeObj.expandNode(pnode);
                    pnode = treeObj.getParentNode(pnode);
                }
            } else {
                mini.alert(data.message);
            }
        }, null, '新增中。。。', true);

    },
    validationNotInChinese: function (title) {
        mini.get(title).setValue(mini.get(title).getValue().replace(/[\u4e00-\u9fa5]/g, ''));
    },
    validationPhone: function (title) {
        var valtxt = mini.get(title).getValue();
        valtxt = valtxt.replace(/[^\d]/g, '');
        while (valtxt.substring(0, 1) == 0 && valtxt.length > 1) {
            valtxt = valtxt.substring(1, valtxt.length);
        }
        mini.get(title).setValue(valtxt);
    },
    validationInt: function (title) {
        var valtxt = mini.get(title).getValue();
        valtxt = valtxt.replace(/[^\d]/g, '');
        while (valtxt.substring(0, 1) == 0 && valtxt.length > 1) {
            valtxt = valtxt.substring(1, valtxt.length);
        }
        if (valtxt.length == 0) {
            valtxt = "0";
        }
        mini.get(title).setValue(valtxt);
    }
}